
local hadc = {}
local adc_id = 0
--[get A] 23.857868020305 B  -5675.6345177665
adc.open(adc_id);
function hadc.adc0_read()
    local adcval, voltval = 0, 0;
    adcval, voltval = adc.read(adc_id);
    if voltval ~= 0xFFFF then
        log.info("test adc2.read", adcval, voltval);
    else
        log.info("test adc2.read error");
    end
    return adcval, voltval;
end

function hadc.volts_read()
    local adcval, voltval = 0, 0;
    local gain, offset = 0, 0
    adcval, voltval = adc.read(adc_id);
    if voltval ~= 0xffff then
        gain = fskv.get("ADC_A") or 1
        offset = fskv.get("ADC_B") or 0
        voltval = gain * adcval + offset
        voltval = voltval / 1000
        -- log.info("test adc2.read", adcval, voltval);
    else
        -- log.info("test adc2.read error");
    end
    return voltval;
end

function hadc.adc0_check(a1, v1, a2, v2)
    local gain, offset = 0, 0
    if a1 == a2 then
        log.info("ADC0 val sample.")
        return -1
    end
    if v1 >= v2 then
        gain = (v1 - v2) * 1000 / (a1 - a2)
        offset = v1 * 1000 - (a1 * gain)
    else
        gain = (v2 - v1) * 1000 / (a2 - a1)
        offset = v1 * 1000 - (a1 * gain)
    end
    log.info("get A", gain, "B ", offset)
    fskv.set("ADC0_A", gain)
    fskv.set("ADC0_B", offset)
    return 0
end

return hadc